package com.lbs.jiepiaisi.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lbs.jiepiaisi.R;
import com.lbs.jiepiaisi.base.BaseActivity;
import com.lbs.jiepiaisi.bean.BaseResp;
import com.lbs.jiepiaisi.manager.SharePreferenceManager;
import com.lbs.jiepiaisi.utils.AccountEvent;
import com.lbs.jiepiaisi.utils.Constant;
import com.lbs.jiepiaisi.utils.UrlUtil;
import com.lbs.jiepiaisi.utils.VinEvent;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.greenrobot.eventbus.EventBus;

import java.lang.reflect.Type;
import java.util.ArrayList;

import static com.lbs.jiepiaisi.manager.App.getContext;

/**
 *
 * 修改密码
 */
public class ResetPwdActivity extends BaseActivity {

    private TextView tv;
    //旧密码输入
    private EditText mPwdEt;
    //新密码
    private EditText mPwdEtNew;
    //确认密码
    private EditText mPwdConfirmEt;
    //提交按钮
    private Button mConfirmBtn;
    //修改类型 0：修改账号密码 1：修改车架号密码
    private int type = 0;
    //
    private SharePreferenceManager mSharePreference=null;
    //
    private String newPwd = "";
    //
    private ArrayList<String> mData = new ArrayList<>();
    //
    private int nowSize = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isTemplate = true;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reset_pwd);
        titleView.setText("修改密码");
        mSharePreference=SharePreferenceManager.getInstance(getContext());
        type = getIntent().getIntExtra("type", 0);
        mData = getIntent().getStringArrayListExtra("mData");
        initView();
        initEvent();
    }

    private void initView() {
        mPwdEt = findViewById(R.id.mPwdEt);
        mPwdEtNew = findViewById(R.id.mPwdEtNew);
        mPwdConfirmEt = findViewById(R.id.mPwdConfirmEt);
        mConfirmBtn = findViewById(R.id.mConfirmBtn);
    }

    private void initEvent() {
        mConfirmBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!mPwdEtNew.getText().toString().equals(mPwdConfirmEt.getText().toString())){
                    Toast.makeText(mContext, "密码不一致", Toast.LENGTH_LONG).show();
                    return;
                }
                if(mPwdEtNew.getText().toString().length() < 6 || mPwdEtNew.getText().toString().length() > 8){
                    Toast.makeText(mContext, "请输入6至8位合法密码", Toast.LENGTH_LONG).show();
                    return;
                }
                if(mPwdEt.getText().toString().equals(mPwdEtNew.getText().toString())){
                    Toast.makeText(mContext, "新密码不能与旧密码一致", Toast.LENGTH_LONG).show();
                    return;
                }
                newPwd = mPwdEtNew.getText().toString();
                switch (type){
                    case 0:
                        resetAccountPwd(mSharePreference.getInt(Constant.ACCOUNT_USERID, 0) + "", mPwdEtNew.getText().toString());
                        break;
                    case 1:
                        for(int i = 0; i<mData.size(); i++){
                            resetVinPwd(mData.get(i), newPwd);
                        }
                        break;
                }
            }
        });
    }

    private void resetVinPwd(String eqId, String password) {
        OkGo.<String>post(UrlUtil.RESET_VIN_PWD)
                .tag(this)
                .retryCount(3)
                .cacheMode(CacheMode.NO_CACHE)
                .params("eqId", eqId)
                .params("password", password)
                .execute(new StringCallback() {

                    @Override
                    public void onSuccess(Response<String> response) {
                        try {
                            Type type = new TypeToken<BaseResp>() {

                            }.getType();
                            BaseResp o = (new Gson().fromJson(response.body(), BaseResp.class));
                            if(o.status == 0){
                                nowSize++;
                                if(nowSize == mData.size()){
                                    Toast.makeText(mContext, "密码修改成功，请重新登录", Toast.LENGTH_LONG).show();
                                    mSharePreference.put(Constant.FRAME_PASSWORD, newPwd);//存入密码
                                    EventBus.getDefault().post(new VinEvent("123"));
                                    finish();
                                }
                            }else{
                                Toast.makeText(mContext, "请输入正确的车架号密码", Toast.LENGTH_LONG).show();
                            }
                        }catch (Exception e){
                            Log.i("tag2", "tag2 =============== " + e.getMessage());
                            Toast.makeText(mContext, "请输入正确的车架号密码", Toast.LENGTH_LONG).show();
                            try {
                                Type type = new TypeToken<BaseResp>() {

                                }.getType();
                                BaseResp o = new Gson().fromJson(response.body(), BaseResp.class);
                                int t = o.status;
//                                if(t == 1)mView.onError("请输入正确的车架号")
//                                else if(t == 3){mView.onError("设备未启用")}
//                                else{
//                                    mView.onError("未知异常")
//                                }
                            }catch (Exception exception){
                                Log.i("tag3", "tag3 =============== " + e.getMessage());
                            }
                        }
                    }
                });

    }

    private void resetAccountPwd(String userId, String password) {
        OkGo.<String>post(UrlUtil.RESET_PWD)
                .tag(this)
                .retryCount(3)
                .cacheMode(CacheMode.NO_CACHE)
                .params("userId", userId)
                .params("password", password)
                .execute(new StringCallback() {

                    @Override
                    public void onSuccess(Response<String> response) {
                        try {
                            Type type = new TypeToken<BaseResp>() {

                            }.getType();
                            BaseResp o = (new Gson().fromJson(response.body(), BaseResp.class));
                            if(o.status == 0){
                                Toast.makeText(mContext, "密码修改成功，请重新登录", Toast.LENGTH_LONG).show();
                                mSharePreference.put(Constant.ACCOUNT_PASSWORD, newPwd);//存入密码
                                EventBus.getDefault().post(new AccountEvent("123"));
                                finish();
                            }else{
                                Toast.makeText(mContext, "请输入正确的账号密码", Toast.LENGTH_LONG).show();
                            }
                        }catch (Exception e){
                            Log.i("tag2", "tag2 =============== " + e.getMessage());
                            Toast.makeText(mContext, "请输入正确的账号密码", Toast.LENGTH_LONG).show();
                            try {
                                Type type = new TypeToken<BaseResp>() {

                                }.getType();
                                BaseResp o = new Gson().fromJson(response.body(), BaseResp.class);
                                int t = o.status;
//                                if(t == 1)mView.onError("请输入正确的车架号")
//                                else if(t == 3){mView.onError("设备未启用")}
//                                else{
//                                    mView.onError("未知异常")
//                                }
                            }catch (Exception exception){
                                Log.i("tag3", "tag3 =============== " + e.getMessage());
                            }
                        }
                    }
                });
    }
}
